Is there a good standard alternative to the
used for determining the size of an array?Code:sizeof(array)/sizeof(array[0])
Well sometimes you see:
but this is only somewhat shorter and not hardly better.Code:sizeof array/sizeof*array
What I'm looking for is a standard macro in a standard header, something like
but there is no such thing in stddef.h where offsetof is defined.Code:#define lenof(a) (sizeof(a)/sizeof((a)[0]))
There is the further problem, that
will return 2 not 8 on most targets, due to the fact that foo is equal toCode:int foo(short array[8]) { return lenof(array); }
which is a rather obscure feature of C.Code:int foo(short *array)